import time
from multiprocessing import Process

#
# # 单线程
# def run1():
#     for i in range(5):
#         print("run()1运行")
#         time.sleep(1)
#
#
# def run2():
#     for i in range(5):
#         print("run()2运行")
#         time.sleep(1)
#
#
# t1 = time.time()
# run1()
# run2()
# t2 = time.time()
# print(t2 - t1)

#
# # 多线程
# def run1():
#     for i in range(5):
#         print("run()1运行")
#         time.sleep(1)
#
#
# def run2():
#     for i in range(5):
#         print("run()2运行")
#         time.sleep(1)
#
#
# if __name__ == '__main__':
#     t1 = time.time()
#     p1 = Process(target=run1)
#     p2 = Process(target=run2)
#     p1.start()
#     p2.start()
#     p1.join()  # 主进程在等待两个子进程执行完毕
#     p2.join()
#     print("主进程")
#     t2 = time.time()
#     print(t2 - t1)


# # 传参
# def run1(num, name='111'):
#     for i in range(num):
#         print(f'{name}函数运行')
#         time.sleep(1)
#
#
# if __name__ == '__main__':
#     # 通常用元组传递参数，元组的效率高于列表，args(args1 , args2)
#     # Process(target=run1, args=(1, '222')).start()
#     Process(target=run1, args=(1,)).start()
#     # Process(target=run1, args=[1, ]).start()
#


# # 单进程变量共享
#
# num = 1
#
#
# def run1():
#     global num  # 声明num为全局变量
#     num = 2
#     print('我是run1函数', num)
#
#
# run1()
# print(num)

# List = []
#
#
# def run1():
#     List.append('lucky')
#     print('我是run1函数',List)
#
#
# run1()
# print(List)


# 多进程变量共享
# 主进程和子进程独立，有自己的独立存储
num = 1


def run1():
    global num
    num = 2
    print('我是run1函数', num)


if __name__ == '__main__':
    p = Process(target=run1)  # 创建了一个子进程 p
    p.start()  # 启动子进程
    p.join()  # 等待子进程执行完毕
    print('子进程over')
    print('输出主进程num', num)
